package com.example.koizzi.ad20180606_note;

import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class InsertActivity extends AppCompatActivity {
    private EditText edName;
    private TextView edage;
    private Button btnInsert;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insert);

        initModule();//初始化组件
        insertStudentData();//插入学生数据,广播
    }

    /*初始化组件 --start--*/
    private void initModule() {
        edName = findViewById(R.id.ed_name);
        edage = findViewById(R.id.ed_age);
        edage.setText(String.valueOf(StudentTable.times()));
        btnInsert = findViewById(R.id.btn_insert);
    }
    /*初始化组件 --end--*/

    /*插入学生数据,广播 --start--*/
    private void insertStudentData() {
        btnInsert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //接收EditText,uri
                Uri uri = Uri.parse(MyContentProvider.URI_STUDENT);
//                int id = Integer.parseInt(edId.getText().toString());
                int id = StudentTable.countss();
                String name = edName.getText().toString();
                int age = StudentTable.times();
                //存储数据至MyContentProvider
                ContentValues values = new ContentValues();
                values.put(StudentTable.COLUMN_ID, id);
                values.put(StudentTable.COLUMN_NAME, name);
                values.put(StudentTable.COLUMN_AGE, age);
                getContentResolver().insert(uri, values);
                //发个广播，插入完毕
                Intent i = new Intent();
                i.setAction("INSERT_FINISHED");
                sendBroadcast(i);
                finish();
            }
        });
    }


    /*插入学生数据,广播 --end--*/
}
